package org.nowcoder.huawei.c100;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 52: 寻找最富裕的小家庭
 */
public class HC052 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        int[] wealth = new int[n];
        String[] vars = br.readLine().split(" ");
        for (int i = 0; i < vars.length; i++) {
            wealth[i] = Integer.parseInt(vars[i]);
        }

        int[][] relations = new int[n][n];
        int count = 1;
        while (count < n) {
            String[] s = br.readLine().split(" ");
            int i = Integer.parseInt(s[0]);
            int j = Integer.parseInt(s[1]);
            relations[i - 1][j - 1] = 1;
            count++;
        }
        solve(wealth, relations, n);
    }

    public static void solve(int[] wealth, int[][] relations, int n) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            int sum = wealth[i];
            for (int j = 0; j < n; j++) {
                if (relations[i][j] == 1) {
                    sum += wealth[j];
                }
            }
            max = Math.max(max, sum);
        }
        System.out.println(max);
    }
}
